package com.techfort.testguice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ResultsActivity extends Activity {
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.results);
		Bundle extras = this.getIntent().getExtras();
		ArrayList<Book> books = extras.getParcelableArrayList("books");
		ListView list = (ListView) findViewById(R.id.results);
		list.setAdapter(new BookAdapter(ResultsActivity.this, R.layout.bookrow,books));
	}
	
	public class BookAdapter extends ArrayAdapter<Book> {

		private List<Book> books;
		public BookAdapter(Context context, int textViewResourceId,
				List<Book> objects) {
			super(context, textViewResourceId, objects);
			books = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if(v==null){
				LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(R.layout.bookrow, null);
			}
			
			final Book book = books.get(position);
			TextView tv = (TextView) v.findViewById(R.id.booktitle);
			tv.setText(book.getTitle());
			v.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					
					Intent intent = new Intent(ResultsActivity.this,Player.class);
					Bundle extra = new Bundle();
					extra.putInt("bookId", book.getId());
					extra.putString("rssurl", book.getRssurl() );
					intent.putExtras(extra);
					startActivity(intent);
				}
			});
			return v;
		}
		
	}
	
}
